<?php
/**
 * Created by PhpStorm.
 * User: yvdedu.com
 * Date: 2019/6/10
 * Time: 13:57
 */

namespace app\api\model;


use think\Db;

class SubjectCourseLog extends BaseModel {


    /*** 修改题目
     * @param $uid
     * @param $course_id
     * @param $status
     * @param $choice
     * @param $car_status
     * @param $course
     * @return false|int
     */
    public function SubjectWhether($uid,$course_id,$status,$choice,$car_status,$course){
         return self::save(['status'=>$status,'update_time'=>time(),'result'=>$choice],function ($query)use ($uid,$course_id,$car_status,$course){
                $query->where('user_id', $uid)->where('course_id',$course_id)->where('car_status',$car_status)->where('course',$course);
            });
    }

    /*** 关联读取type表
     * @param $uid
     * @param $course
     * @param $car_status
     * @return array|false
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function SubjectLog($uid,$course,$car_status){
        $status =[1,$car_status*1 ];
        $subject = Db::table('xhy_subject_course')
            ->alias('a')
            ->join('xhy_subject_type b','a.content_type = b.id')
            ->where('a.course',$course)
            ->where('b.status','in',$status)
            ->select()->toArray();
        $data = [];
        foreach ( $subject as $k=>$v ){
            $data[] = [
                'user_id'=>$uid,
                'car_status'=>$car_status,
                'course'=>$subject[$k]['course'],
                'course_id'=>$subject[$k]['c_id'],
                'create_time'=>time()
            ];
        }
        $result = $this->isUpdate(false)->saveAll($data,false);
        return $result;
    }

    /*** 正错题的数量
     * @param $uid
     * @param $course
     * @param $status
     * @param $car_status
     * @return int|string
     * @throws \think\Exception
     */
    public function SubjectDecide($uid,$course,$status,$car_status){
        return self::where([
                        'user_id'=>$uid,
                        'course'=>$course,
                        'status'=>$status,
                        'car_status'=>$car_status
                        ])
            ->count('status');
    }



    /*** 用户做的科目1/4的题
     * @param $uid
     * @param $course
     * @param $car_status
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function SubjectList($uid,$course,$car_status){
        $data = Db::table('xhy_subject_course_log')
                        ->alias('a')
                        ->join('xhy_subject_course b','a.course_id = b.c_id')
                        ->join('xhy_subject_type c','c.id = b.content_type')
                        ->where('a.course','=',$course)
                        ->where('a.car_status','=',$car_status)
                        ->where('a.user_id','=',$uid)
                        ->field([
                            'a.status','a.result','b.content_type','b.content','b.content_img','b.content_gif',
                            'b.item1','b.item2','b.item3','b.item4','b.item_true','b.course','b.ask_txt','b.ask_img'
                            ,'b.culture','b.key_words','b.key_true','b.key_false','b.audio','b.video','c.name','b.c_id'
                        ])
                        ->select()->toArray();
        $url = 'https://resource.17jxyx.com/';
        foreach ( $data as $k=>$v ){
            if (!empty($v['content_img'])){
                $data[$k]['content_img'] = $url.$v['content_img'];
            }
            if (!empty($v['content_gif'])){
                $data[$k]['content_gif'] = $url.$v['content_gif'];
            }
            if (!empty($v['ask_img'])){
                $data[$k]['ask_img'] = $url.$v['ask_img'];
            }
            if (!empty($v['audio'])){
                $data[$k]['audio'] = $url.$v['audio'];
            }
            if (!empty($v['video'])){
                $data[$k]['video'] = $url.$v['video'];
            }
        }
        return $data;
    }


}